package domain;

public class Similarity {
	private User user1;
	private User user2;
	private double averageUser1;
	private double averageUser2;
	private double result1 = 0;
	private double result2 = 0;
	private double result3 = 0;
	private double sim = 0;

	public Similarity(User user1, User user2) {
		this.user1 = user1;
		this.user2 = user2;
	}

	public double calculateSimilarity() {

		averageUser1 = calculateAverageRating(user1);
		averageUser2 = calculateAverageRating(user2);

		for (int i = 0; i < user1.getPreference().getRatingList().size(); i++) {
			result1 = result1
					+ ((user1.getPreference().getRatingList().get(i).getRate() - averageUser1) * (user2
							.getPreference().getRatingList().get(i).getRate() - averageUser2));

			result2 = result2
					+ ((user1.getPreference().getRatingList().get(i).getRate() - averageUser1) * (user1
							.getPreference().getRatingList().get(i).getRate() - averageUser1));

			result3 = result3
					+ ((user2.getPreference().getRatingList().get(i).getRate() - averageUser2) * (user2
							.getPreference().getRatingList().get(i).getRate() - averageUser2));

			this.sim = result1 / Math.sqrt(result2 * result3);
		}
		return this.sim;
	}

	public double calculateAverageRating(User user) {
		double sumRating = 0;

		for (int i = 0; i < user.getPreference().getRatingList().size(); i++) {
			sumRating = sumRating
					+ user.getPreference().getRatingList().get(i).getRate();
		}
		sumRating = sumRating / user.getPreference().getRatingList().size();

		return sumRating;
	}

	public double getAverageUser1() {
		return averageUser1;
	}

	public void setAverageUser1(double averageUser1) {
		this.averageUser1 = averageUser1;
	}

	public double getAverageUser2() {
		return averageUser2;
	}

	public void setAverageUser2(double averageUser2) {
		this.averageUser2 = averageUser2;
	}

	public double getSim() {
		return sim;
	}

	public void setSim(double sim) {
		this.sim = sim;
	}

	public User getUser1() {
		return user1;
	}

	public void setUser1(User user1) {
		this.user1 = user1;
	}

	public User getUser2() {
		return user2;
	}

	public void setUser2(User user2) {
		this.user2 = user2;
	}
	
	

}
